package com.xywl.escort.view.setting

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.xywl.escort.R
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.common.PreferenceKeys
import com.xywl.escort.utils.RxSPTool
import com.xywl.escort.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_contact_us.*

@Route(path = "/xy/setting/contact")
class ContactUsActivity : BaseActivity() {

    private var mDevHitCountdown: Int = 0
    private val TAPS_TO_BE_A_DEVELOPER = 7

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_contact_us)
        showBackButton()
        setTitleText("联系我们")

        text_business_cooperation.setOnClickListener {
            call("400-666-8888")
        }
        text_contact_phone.setOnClickListener {
            call("400-666-8888")
        }

        mDevHitCountdown = TAPS_TO_BE_A_DEVELOPER

        img_icon.setOnClickListener {
            val isDev = RxSPTool.getBoolean(this@ContactUsActivity, PreferenceKeys.IS_DEV)
            if (isDev) {
                ToastUtil.showTip(this@ContactUsActivity, getString(R.string.show_dev_already))
            } else {
                if (mDevHitCountdown > 0) {
                    mDevHitCountdown--
                    if (mDevHitCountdown == 0) {
                        mDevHitCountdown++
                        ARouter.getInstance().build("/xy/verification").navigation()
                    } else if (mDevHitCountdown > 0 && mDevHitCountdown < TAPS_TO_BE_A_DEVELOPER - 2) {
                        ToastUtil.showTip(this@ContactUsActivity, resources.getQuantityString(R.plurals.show_dev_countdown, mDevHitCountdown, mDevHitCountdown))
                    }
                } else if (mDevHitCountdown < 0) {
                    ToastUtil.showTip(this@ContactUsActivity, getString(R.string.show_dev_already))
                }
            }
        }
    }

    private fun call(phone: String) {
        val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$phone"))
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        startActivity(intent)
    }
}
